import {
	api
} from "@/utils/api.js"
const fn = {
	calling(phoneNumber) {
		uni.makePhoneCall({
			phoneNumber
		});
	},
	pay(payinfo, callback) {
		uni.requestPayment({
			provider: 'wxpay',
			appId: payinfo.appId,
			nonceStr: payinfo.nonceStr,
			package: payinfo.package,
			paySign: payinfo.paySign,
			signType: payinfo.signType,
			timeStamp: payinfo.timeStamp,
			success: function(res) {
				callback(res)
			},
			fail: function(err) {
				console.log("err: ", err);
				uni.showToast({
					icon: 'error',
					title: '支付失败',
					duration: 2000
				});
			}
		});
	},
	// 根据授权获取登录需要的用户信息
	login(callback) {
		let wechatCode = ""
		let userInfo = {}
		uni.login({
			provider: 'weixin',
			success: loginRes => {
				wechatCode = loginRes.code;
			},
			fail: fail => {
				console.log('loginRes.fail', fail);
			}
		});
		uni.getUserProfile({
			desc: '声明获取用户个人信息后的用途',
			success: ({
				encryptedData,
				iv,
				signature
			}) => {
				this.handleLogin({
					encryptedData,
					iv,
					signature,
					wechatCode
				}).then(res => {
					callback(res)
				})
			},
			fail: fail => {
				console.log('loginRes.fail', fail);
			}
		});
	},
	// 执行登陆操作
	async handleLogin({
		encryptedData,
		iv,
		signature,
		wechatCode
	}) {
		return await api.login({
			code: wechatCode,
			encryptedData,
			iv,
			signature
		});
	},
}
export {
	fn
}
